import gc
import weakref


class ExpensiveObject:

    def __del__(self):
        print('(Deleting {}'.format(self))

    def on_finalize(self):
        print('do finalize')


obj = ExpensiveObject()
obj_id = id(obj)

f = weakref.finalize(obj, obj.on_finalize)
# f.atexit = False

del obj
print(f)

for o in gc.get_objects():
    if id(o) == obj_id:
        print('found uncollected object in gc')